package cn.com.thinvent.dispatch.web.controller;

import cn.com.thinvent.dispatch.entity.response.ReturnModel;
import cn.com.thinvent.dispatch.service.biz.ShoppingCartService;
import cn.com.thinvent.dispatch.service.biz.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("/batchAdd")
    public ReturnModel batchAddCarts(@RequestParam(value = "cards") String cards) {
        String[] split = cards.split(",");
        List<String> carts = new ArrayList<>(Arrays.asList(split));
        return new ReturnModel(200, shoppingCartService.addCarts(carts), "Success");
    }

    @GetMapping("/list")
    public ReturnModel getCartsByUserId() {
        return new ReturnModel(200, shoppingCartService.getCartsByUserIdPaged(UserContext.get(), 1, 20), "Success");
    }

    @DeleteMapping("/delete")
    public ReturnModel deleteByMenuIdAndCreatedById(@RequestParam String menuId, @RequestParam String createdById) {
        int result = shoppingCartService.deleteByMenuIdAndCreatedById(menuId, createdById);
        if (result > 0) {
            return new ReturnModel(200, result, "Success");
        } else {
            return new ReturnModel(200, result, "Fail");
        }
    }

}
